from django.db import models
from shop.models import course
from user.models import user


# Create your models here.
class cart(models.Model):
    user = models.ForeignKey(user, on_delete=models.CASCADE, verbose_name="用户", related_name='cartuser')
    course = models.ForeignKey(course, on_delete=models.CASCADE, verbose_name="课程", related_name='cartcourse')
    isselect = models.BooleanField(default=True, verbose_name="是否选中")
    amount = models.IntegerField(default=0)

    class Meta:
        db_table = "cart"


class coupon(models.Model):
    name = models.CharField(max_length=30, verbose_name="优惠券名称")
    price = models.IntegerField(default=0, verbose_name="优惠金额")
    condition = models.IntegerField(default=0, verbose_name="满足多少金额可以使用")
    number = models.IntegerField(default=0, verbose_name="优惠券数量")
    isdel = models.BooleanField(default=False, verbose_name="是否可用")
    starttime = models.DateTimeField(verbose_name="开始时间")
    endtime = models.DateTimeField(verbose_name="结束时间")
    intro = models.CharField(max_length=200, verbose_name="优惠券介绍", null=True, blank=True)

    class Meta:
        db_table = "coupon"


class usercoupon(models.Model):
    user = models.ForeignKey(user, on_delete=models.CASCADE, verbose_name="优惠券关联用户", related_name="couponuser")
    coupon = models.ForeignKey(coupon, on_delete=models.CASCADE, verbose_name="用户关联的优惠券",
                               related_name="usercoupon")
    isused = models.BooleanField(default=False, verbose_name="是否用过了")
    addtime = models.DateTimeField(auto_now_add=True, verbose_name="优惠券添加时间")
    usetime = models.DateTimeField(verbose_name="使用时间", null=True, blank=True)

    class Meta:
        db_table = "usercoupon"


status = ((0, "未付款"), (1, "已付款"), (2, "已取消"), (3, "已完成"), (4, "退款中"), (5, "已退款"))


# 订单表
class order(models.Model):
    orderno = models.CharField(max_length=200, verbose_name="订单号算法自动生成")
    user = models.ForeignKey(user, on_delete=models.CASCADE, verbose_name="订单对应的用户")
    totalprice = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="订单总金额")
    payprice = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="订单支付金额")
    addtime = models.DateTimeField(auto_now_add=True, verbose_name="下单时间")
    paytime = models.DateTimeField(verbose_name="实际支付时间", null=True, blank=True)
    status = models.IntegerField(default=0, choices=status, verbose_name="订单状态")
    usercoupon = models.ForeignKey(usercoupon, on_delete=models.SET_NULL, null=True, blank=True,
                                   verbose_name="用户优惠券")
    score = models.IntegerField(default=0, verbose_name="用户使用的积分")
    paytype = models.CharField(default="wx", max_length=50, verbose_name="支付方式")
    payno = models.CharField(max_length=200, verbose_name="支付流水", null=True, blank=True)

    class Meta:
        db_table = "order"


class orderdetail(models.Model):
    order = models.ForeignKey(order, on_delete=models.CASCADE, verbose_name="订单号")
    title = models.CharField(max_length=200, default='', verbose_name="课程名称")
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="课程原价")
    realprice = models.DecimalField(max_digits=10, decimal_places=2, verbose_name="折后价格")
    students = models.IntegerField(default=0, verbose_name="报名学生人数")
    level = models.IntegerField(choices=((0, '初级'), (1, '中级'), (2, '高级')), default=0, verbose_name="课程等级")
    intro = models.CharField(max_length=200, verbose_name="课程介绍", default='', )
    isfinish = models.BooleanField(default=False, verbose_name="是否完成")
    finishtime = models.DateTimeField(auto_now_add=False, verbose_name="完成时间")
    score = models.IntegerField(default=0, verbose_name="分数")
    detail = models.CharField(max_length=200, default='', verbose_name="课程详情")
    cover = models.CharField(max_length=200, null=True, blank=True, verbose_name="封面图片")
    isfree = models.BooleanField(default=False, verbose_name="是否免费")

    class Meta:
        db_table = "orderdetail"

    def __str__(self):
        return self.title
